package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RankClassAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.db.SongListEntity;
import com.lanou.sfu.songforu.entity.ArtistDetailSongEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.tools.ListViewForScrollView;
import com.lanou.sfu.songforu.tools.SwipeRefreshLoadingLayout;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/27.
 */
public class ArtistDetailSongFragment extends BaseFragment implements RankClassAdapter.OnClickToShareLinstener, SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener {
    private View v;
    private ListViewForScrollView listView;
    private VolleySingleton requestQueue;
    private StringRequest request;
    private String tinguid;
    private Context context;
    private List<ArtistDetailSongEntity.SonglistEntity> entityList;
    private List<SongListEntity> changeList;
    private UMSocialService mController;
    private Button playAll;
    private ImageButton download, share;
    private int times = 0;
    private int offset = 20;
    private String url;
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;
    private RankClassAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_artist_detail_song, null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Qzone分享
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(), getString(R.string.qQ_appID), getString(R.string.qQ_appSecret));
        qZoneSsoHandler.addToSocialSDK();

        // qq分享
        UMQQSsoHandler qqssoH = new UMQQSsoHandler(getActivity(), getString(R.string.qQ_appID), getString(R.string.qQ_appSecret));
        qqssoH.addToSocialSDK();

        // 设置分享内容
        QQShareContent qqcontent = new QQShareContent();
        qqcontent.setShareContent(context.getString(R.string.SHARE_CONTENT));
        mController.setShareMedia(qqcontent);

        Bundle bundle = getArguments();
        tinguid = bundle.getString(getString(R.string.id));
        // &offset=提出用于加载
        url = Url.ARTIST_SONG_TINGUID + tinguid;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ArtistDetailSongEntity artistDetailSongEntity = gson.fromJson(response, ArtistDetailSongEntity.class);
                entityList = artistDetailSongEntity.getSonglist();
                for (ArtistDetailSongEntity.SonglistEntity entity : entityList) {
                    SongListEntity songListEntity = new SongListEntity();
                    songListEntity.setAlbum_id(entity.getAlbum_id());
                    songListEntity.setAll_artist_id(entity.getAll_artist_id());
                    songListEntity.setTitle(entity.getTitle());
                    songListEntity.setTing_uid(entity.getTing_uid());
                    songListEntity.setLanguage(entity.getLanguage());
                    songListEntity.setAuthor(entity.getAuthor());
                    songListEntity.setLrclink(entity.getLrclink());
                    songListEntity.setSong_id(entity.getSong_id());
                    songListEntity.setArtist_name(entity.getAuthor());
                    songListEntity.setArtist_id(entity.getArtist_id());
                    songListEntity.setPic_big(entity.getPic_big());
                    changeList.add(songListEntity);
                }

                adapter = new RankClassAdapter(context, changeList);
                adapter.setOnClickToShareLinstener(ArtistDetailSongFragment.this);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.addQueue(request);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (ArtistDetailSongEntity.SonglistEntity entity : entityList) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_big());
                    musicList.setFrom(context.getString(R.string.NET));
                    lists.add(musicList);
                }
                MainActivity.play(lists, position);
            }
        });
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mController.openShare(getActivity(), false);
            }
        });

        playAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (ArtistDetailSongEntity.SonglistEntity entity : entityList) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
                    musicList.setSingerImage(entity.getPic_big());
                    musicList.setFrom(getString(R.string.NET));
                    lists.add(musicList);
                }
                MainActivity.play(lists, 0);
            }
        });

        swipeRefreshLoadingLayout.setOnLoadListener(this);

    }

    private void init() {
        context = getActivity();
        requestQueue = VolleySingleton.getInstance(context);
        listView = (ListViewForScrollView) v.findViewById(R.id.artist_detail_song_listview);
        changeList = new ArrayList<SongListEntity>();
        playAll = (Button) v.findViewById(R.id.artist_detail_song_playAll);
//        download = (ImageButton) v.findViewById(R.id.artist_detail_song_download);
        share = (ImageButton) v.findViewById(R.id.artist_detail_song_share);
        mController = UMServiceFactory.getUMSocialService(getString(R.string.UMSocialService));
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) v.findViewById(R.id.refresh_load);
    }

    @Override
    public void onClickToShare(SongListEntity songListEntity, int position) {
        mController.openShare(getActivity(), false);
    }

    @Override
    public void onLoad() {
        times++;
        String urlLoad = url + Url.ARTIST_SONG_OFFSET + (times * offset);
        request = new StringRequest(urlLoad, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ArtistDetailSongEntity artistDetailSongEntity = gson.fromJson(response, ArtistDetailSongEntity.class);
                List<ArtistDetailSongEntity.SonglistEntity> newEntityList = artistDetailSongEntity.getSonglist();

                for (ArtistDetailSongEntity.SonglistEntity entity : newEntityList) {
                    SongListEntity songListEntity = new SongListEntity();
                    songListEntity.setAlbum_id(entity.getAlbum_id());
                    songListEntity.setAll_artist_id(entity.getAll_artist_id());
                    songListEntity.setTitle(entity.getTitle());
                    songListEntity.setTing_uid(entity.getTing_uid());
                    songListEntity.setLanguage(entity.getLanguage());
                    songListEntity.setAuthor(entity.getAuthor());
                    songListEntity.setLrclink(entity.getLrclink());
                    songListEntity.setSong_id(entity.getSong_id());
                    songListEntity.setArtist_name(entity.getAuthor());
                    songListEntity.setArtist_id(entity.getArtist_id());
                    songListEntity.setPic_big(entity.getPic_big());
                    changeList.add(songListEntity);
                }
                entityList.addAll(newEntityList);
                adapter.addDatas(changeList);
                adapter.setOnClickToShareLinstener(ArtistDetailSongFragment.this);
                swipeRefreshLoadingLayout.setLoading(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.addQueue(request);

    }

    @Override
    public void onRefresh() {
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ArtistDetailSongEntity artistDetailSongEntity = gson.fromJson(response, ArtistDetailSongEntity.class);
                entityList = artistDetailSongEntity.getSonglist();
                for (ArtistDetailSongEntity.SonglistEntity entity : entityList) {
                    SongListEntity songListEntity = new SongListEntity();
                    songListEntity.setAlbum_id(entity.getAlbum_id());
                    songListEntity.setAll_artist_id(entity.getAll_artist_id());
                    songListEntity.setTitle(entity.getTitle());
                    songListEntity.setTing_uid(entity.getTing_uid());
                    songListEntity.setLanguage(entity.getLanguage());
                    songListEntity.setAuthor(entity.getAuthor());
                    songListEntity.setLrclink(entity.getLrclink());
                    songListEntity.setSong_id(entity.getSong_id());
                    songListEntity.setArtist_name(entity.getAuthor());
                    songListEntity.setArtist_id(entity.getArtist_id());
                    songListEntity.setPic_big(entity.getPic_big());
                    changeList.add(songListEntity);
                }
                adapter = new RankClassAdapter(context, changeList);
                listView.setAdapter(adapter);
                swipeRefreshLoadingLayout.setRefreshing(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.addQueue(request);
        adapter.notifyDataSetChanged();
    }
}
